package com.l024.easyandroidlib;

import android.app.Application;
import android.content.Context;
import android.text.TextUtils;

import com.apkfuns.log2file.LogFileEngineFactory;
import com.apkfuns.logutils.LogLevel;
import com.apkfuns.logutils.LogUtils;
import com.hjq.toast.ToastUtils;
import com.tencent.mmkv.MMKV;

/**
 * 初始化工具
 */
public class EasyAndroid {
    private static EasyAndroid easyAndroid = null;
    private static Context mContext = null;

    private EasyAndroid(){

    }

    public static EasyAndroid getInstance(){
        if(easyAndroid==null){
            synchronized (EasyAndroid.class){
                if(easyAndroid==null){
                    easyAndroid = new EasyAndroid();
                }
            }
        }
        return easyAndroid;
    }

    /**
     * 初始化
     * @param context
     */
    public static void init(Context context,String logPath){
        mContext = context;
        //初始化toast
        // 在Application中初始化
        ToastUtils.init((Application)context);
        //初始化日志
        initLog(context,logPath);
        //初始化EasyLog
//        EasyLogManager.init(new EasyLogConfig() {
//            @Override
//            public String getGlobalTag() {
//                return super.getGlobalTag();
//            }
//
//            @Override
//            public boolean enable() {
//                return super.enable();
//            }
//
//            @Override
//            public EasyLogPrinter[] printers() {
//                return super.printers();
//            }
//        });
        //初始化MMKV
        String cacheDir = MMKV.initialize(context);
    }

    /**
     * 初始化日志
     * @param context
     */
    private static void initLog(Context context,String logPath) {
        if(TextUtils.isEmpty(logPath)){
            logPath = "/sdcard/EasyAndroid/logs/";
        }
        LogUtils.getLogConfig()
                .configAllowLog(true)
                .configTagPrefix("EasyAndroidLog")
                .configShowBorders(true)
                .configFormatTag("%d{HH:mm:ss:SSS} %t %c{-5}");

//        支持写入日志到文件
        LogUtils.getLog2FileConfig().configLog2FileEnable(true)
                // targetSdkVersion >= 23 需要确保有写sdcard权限
                .configLog2FilePath(logPath)
                .configLog2FileNameFormat("log_%d{yyyyMMdd}.txt")
                .configLog2FileLevel(LogLevel.TYPE_ERROR)
                .configLogFileEngine(new LogFileEngineFactory(context));
    }

    public Context getContext(){
        return mContext;
    }
}
